<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="documentationForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="documentationForm.title" />
      </el-form-item>
      <el-form-item label="栏目" style="margin-bottom: 40px;" prop="columnID">
        <el-select v-model="documentationForm.columnID" filterable clearable remote :remote-method="loadColumnList" placeholder="请选择">
          <el-option
            v-for="item in columnOptions"
            :key="item.ID"
            :label="item.title"
            :value="item.ID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="documentationForm.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="描述" />
      </el-form-item>
      <el-form-item label="封面图片">
        <Upload v-model="documentationForm.logo" />
      </el-form-item>
      <el-form-item label="投放渠道" style="margin-bottom: 40px;" prop="columnID">
        <el-select v-model="documentationForm.columnID" filterable clearable remote :remote-method="loadColumnList" placeholder="请选择">
          <el-option
            v-for="item in columnOptions"
            :key="item.ID"
            :label="item.title"
            :value="item.ID"
          />
        </el-select>
      </el-form-item>
      
        <el-form-item prop="正文" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="documentationForm.content" :height="400" />
        </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="documentationForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="documentationForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="documentationForm.ID > 0 && documentationForm.status.value < 6" type="danger" @click="handleDelete(documentationForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { saveDocumentation, removeDocumentationByID, getDocumentationDetail } from '@/api/popularize/documentation'
import { getColumnUsableList } from '@/api/popularize/column'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload,Tinymce },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      columnOptions: [],
      documentationForm: {
        title: '',
        link: '',
        remark: '',
        status: {
          label: '',
          value: 1
        },
        style: {
          label: '',
          value: 1
        }
      },
      rules: []
    }
  },
  created() {
    this.loadColumnList();
    let documentationID = this.$route.query && this.$route.query.documentationID
    if (documentationID !== undefined) {
      documentationID = parseInt(documentationID)
      if (!isNaN(documentationID)) {
        this.listLoading = true
        const formData = {
          'documentationID': documentationID
        }
        getDocumentationDetail(formData).then(response => {
          if (response.status === 200) {
            this.documentationForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(documentationID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'documentation' })
    },
    loadColumnList:function(columnName){
      let _this = this;
      let formData = {}
      if(columnName != undefined){
        formData['keyword'] = columnName;
      }
      getColumnUsableList(formData).then((res)=>{
        if(res.status == 200){
            _this.columnOptions = res.data.list;
        }
      })

    },
    createData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.documentationForm)
      formData.status = 6

      if (formData.ID > 0) {
        formData.documentationID = formData.ID
      }
      saveDocumentation(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'documentation' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      }).catch((res)=>{
        console.log(res)
      })
    },
    draftData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.documentationForm)
      formData.status = 1
      formData.style = formData.style.value

      if (formData.ID > 0) {
        formData.documentationID = formData.ID
      }
      saveDocumentation(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'documentation' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.listLoading = true

      removeDocumentationByID(this.listQuery).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'documentation' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
